#include <bits/stdc++.h>
#define int long long
using namespace std;
int t, n, ans, a[200005], k[200005], dp[200005], maxn[200005], v[200005], kg[200005];
vector<int> g[1000005];
signed main()
{
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  freopen("color.in", "r", stdin);
  freopen("color.out", "w", stdout);
  cin >> t;
  while (t--)
  {
    memset(v, 0, sizeof v);
    memset(dp, 0, sizeof dp);
    memset(maxn, 0, sizeof maxn);
    memset(k, 0, sizeof k);
    memset(kg, 0, sizeof kg);
    for (int i = 1; i <= 1e6; i++)
      g[i].clear();
    cin >> n, ans = 0;
    for (int i = 1; i <= n; i++)
    {
      cin >> a[i];
      if (a[i] == a[i - 1])
      {
        ans += a[i];
        i--, n--;
        continue;
      }
      g[a[i]].push_back(i), k[i] = g[a[i]].size() - 1;
    }
    for (int i = 1; i <= n; i++)
      if (k[i])
      {
        int lst = g[a[i]][k[i] - 1];
        if (k[lst + 1])
          v[lst] = g[a[lst + 1]][k[lst + 1] - 1];
      }
    for (int i = 1; i <= n; i++)
    {
      maxn[i] = maxn[i - 1];
      dp[i - 1] = max(dp[i - 1], maxn[i - 1]);
      if (k[i])
      {
        int lst = g[a[i]][k[i] - 1], lt = -1;
        if (k[lst + 1])
          lt = g[a[lst + 1]][k[lst + 1] - 1];
        dp[i - 1] = max(dp[i - 1], maxn[lst] + a[i]);
        if (v[lst])
          dp[i - 1] = max(dp[i - 1], kg[lst] + a[lst + 1] + a[i]);
      }
      maxn[i] = max(maxn[i], dp[i - 1]);
      if (v[i])
        kg[i] = dp[v[i]];
    }
    cout << maxn[n] + ans << '\n';
  }
  exit(0);
}
